package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.NjcbB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.NjcbContants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.njcb.ebank.NjcbB2cMessage;
import com.gopay.common.domain.bank.njcb.ebank.query.request.NjcbeBankDataReq;
import com.gopay.common.domain.bank.njcb.ebank.query.response.NjcbeBankDataRep;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class NjcbB2cBankServiceImpl implements BankInteractRemoteService {

	@Resource(name="njcbB2cManager")
	private NjcbB2cManager njcbB2cManager;
	
	@Override
	public Result pay(BaseMessage baseMessage) {
		Result result = new Result(false);
		try {
			NjcbB2cMessage messageB2C = new NjcbB2cMessage();
			baseMessage.setBankOrderId(baseMessage.getOrderId());
			BeanUtils.copyProperties(baseMessage,messageB2C);
			NjcbB2cMessage resultMsg = (NjcbB2cMessage)njcbB2cManager.sign(messageB2C);
			result.setDefaultModel(resultMsg);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}
	
	@Override
	public Result bankBack(BaseMessage baseMessage) {
		Result result = new Result(false);        
        try {
        	boolean flag = false;
        	NjcbB2cMessage messageB2C = new NjcbB2cMessage();
        	BeanUtils.copyProperties(baseMessage,messageB2C);
            flag = (Boolean)njcbB2cManager.verify(messageB2C);
            result.setModel("message", messageB2C);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            // 验签成功入账
            if (result.isSuccess() && flag) {
            	Map<String, String> param = new HashMap<String, String>();
                // 银行返回成功状态
                if (NjcbContants.ORDER_TRANS_RESULT_SUCC.equals(messageB2C.getTranResult())) {                    
                    param.put("orderId", messageB2C.getOrderId());
                    param.put("amt", messageB2C.getMerOrderAmt());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "00");
                    param.put("retCode", "00");
                    param.put("retMsg", "验签成功");
                    param.put("signData", messageB2C.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(true);
                } else {
                    // 通知后台地址该订单失败
                    param.put("orderId", messageB2C.getOrderId());
                    param.put("amt", messageB2C.getMerOrderAmt());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "01");
                    param.put("retCode", "01");
                    param.put("retMsg", "验签失败");
                    param.put("signData", messageB2C.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(false);
                }
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
	}

	@Override
	public Result query(BaseMessage baseMessage) {
		Result result = new Result(false);
		NjcbeBankDataReq messageB2C = new NjcbeBankDataReq();
		BeanUtils.copyProperties(baseMessage,messageB2C);
        try {
        	NjcbeBankDataRep orderRes = (NjcbeBankDataRep)njcbB2cManager.query(messageB2C);
	        result.setDefaultModel(orderRes);
	        result.setSuccess(true);	
	        if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
	    		Map<String, String> param = new HashMap<String, String>();
	    		param.put("orderId", messageB2C.getOrderId());
	    		param.put("amount", messageB2C.getAmount().toString());
	    		if (orderRes == null) {
					// 查询订单为空
	    			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	    			param.put("returnMessage", "银行返回对账结果为空");
	    			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (!NjcbContants.QUERY_ORDER_TRANS_SUCCESS.equals(orderRes.getOpRep().getRetCode())) {
					// 查询订单错误
	    			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	    			param.put("returnMessage", "银行返回对账结果为空");
	    			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				//成功处理
				if (StringUtils.equals(NjcbContants.ORDER_TRANS_RESULT_SUCC,orderRes.getOpRep().getOpResult().getOpResultSet().getTranResult())) {
    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				}
				//失败处理
				else if(StringUtils.equals(NjcbContants.ORDER_TRANS_RESULT_FAIL,orderRes.getOpRep().getOpResult().getOpResultSet().getTranResult())){
					String errorMsg = orderRes.getOpRep().getErrMsg();
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
					param.put("returnMessage", errorMsg);
				}
				//不处理
				else {
					String errorMsg = orderRes.getOpRep().getErrMsg();
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", errorMsg);
				}
	    		result.setModel("queryResult", param);
	    	}
		}catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
        return result;
	}
	
	@Override
	public Result dayend(BaseMessage baseMessage) {
		return null;
	}

	@Override
	public Result refund(BaseMessage baseMessage) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
